<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

/**
 * 购物车项处理(商品)
 * $ 2010-04-28 19:46 $
 */
class b2c_gallery_filter{

    private $redis = '';
    private $db = '';
    private $omath = '';
    /**
     * 构造函数
     *
     * @param $object $app  // service 调用必须的
     */
    public function __construct() {
        $this->app = app::get('b2c');

        $this->db = kernel::database();
        $this->redis = kernel::single('base_kvstore_redis2');
        $this->redis->selects(1);
        $this->omath = kernel::single('ectools_math');
    }


    //优化左侧筛选 start
    public function filter_ajax($_POST){
        //-----获取虚拟分类----------
        if($_POST['cat_id']==0 && $_POST['virtual_id']>0){
            $this->_get_virtual($_POST);
        }
        //-----获取全部-------
        if($_POST['cat_id']==0 && $_POST['virtual_id']==0){
            $this->_get_all($_POST);
        }
        //-----获取某个分类-------
        if($_POST['cat_id']!=0  && $_POST['virtual_id']==0 ){
            $this->_get_cat($_POST);
        }
    }

    private function _filter_props($params, $data=false){
        set_time_limit(0);

        $getpost = $params;
        //获取当前点击的属性
        $select_id = $getpost['select_id'] ;
        $select_type = $getpost['select_type'] ;
        if($select_type){
            $selects_key = substr($select_id, 0, strpos($select_id,'-'));
        }else{
            $arr = explode('-',end(explode(',',$getpost['props_value_id'])));
            $selects_key = $arr[0];
        }
        $selects[$selects_key] = substr( $select_id,(strpos($select_id,'-')+1) );

        if( $selects_key == 'price' ){
            return  true;
        }
        $params = explode(',',$params['props_value_id']);
        foreach ($params as $key => $value) {
            $arr = explode('-',$value);
            if (substr($arr[0], 0, 2) == 'p_') {
                if($select_type == 0) {
                    if ($arr[0] == $selects_key) {
                        $props_select[substr($arr[0], 2)][$arr[1]] = $arr[1];
                    }
                }
                $props[substr($arr[0], 2)][$arr[1]] = $arr[1];
            }
            //品牌
            if($arr[0]=='brand_id'){
                if($select_type == 0) {
                    if ($arr[1] == $selects[$selects_key]) {
                        $brand_selecet[$arr[1]] = $arr[1];
                    }
                }
                $brand[$arr[1]] = $arr[1];
            }
            unset($arr);
        }

        if($data===1) {
            //虚拟分类
            $cats_goods = $this->redis->setMembers('virtual_goods_'.$getpost['virtual_id']);
            if(!$cats_goods){
                $cats_goods = $this->redis->setMembers('cat_3');
            }
        }else{
            //当前分类的商品
            if(!$getpost['cat_id'] || $getpost['cat_id']=='NaN') {
                $cats_goods = $this->redis->setMembers('cat_3');
            }else{
                $cats_goods = $this->redis->setMembers('cat_'.$getpost['cat_id']);
            }
        }
        //获取所有的属性
        $this->redis->fetch('props',$props_obj);
        $props_obj = json_decode($props_obj, true);
        //获取所有的品牌
        $this->redis->fetch('brands',$brand_obj);
        $brand_obj = explode(',',$brand_obj);

        //选中品牌
        if($brand){
            foreach($brand as $_bkey=>$_bval){
                $brands_goods = 'brand_' . $_bval;
                $brands_goods_all[$_bval] = $brands_goods;
                unset($brands_goods);
            }
            if(count($brands_goods_all)>1) {
                $brands_goods_alls = $this->redis->setUnion($brands_goods_all);
            }else if(count($brands_goods_all)==1){
                $brands_goods_alls = $this->redis->setMembers(current($brands_goods_all));
            }
        }

        if($props) {
            foreach ($props as $_pk => $_pv) {
                foreach($_pv as $_pkey=>$_pval) {
                    $props_goods = 'props_' . $_pval;
                    $props_goods_all[] = $props_goods;
                    unset($props_goods);
                }
                if(count($props_goods_all)>1) {
                    $props_goods_alls1[$_pk] = $this->redis->setUnion($props_goods_all);
                }else if(count($props_goods_all) == 1){
                    $props_goods_alls1[$_pk] =  $this->redis->setMembers(current($props_goods_all));
                }
                unset($props_goods_all);
            }
            if(count($props_goods_alls1)>1) {
                $props_goods_alls = array_unique(call_user_func_array('array_intersect', $props_goods_alls1));
                if(substr($selects_key, 0, 2) == 'p_'){
                    unset($props_goods_alls1[substr($selects_key, 2)]);
                    if($brands_goods_alls){
                        array_push($props_goods_alls1,$brands_goods_alls,$cats_goods);
                        $props_goods_alls_selected = array_unique(call_user_func_array('array_intersect', $props_goods_alls1));
                    }else{
                        array_push($props_goods_alls1,$cats_goods);
                        $props_goods_alls_selected = array_unique(call_user_func_array('array_intersect', $props_goods_alls1));
                    }
                }
            }else if(!empty($props_goods_alls1) && count($props_goods_alls1)==1){
                $props_goods_alls = current($props_goods_alls1);
                if(substr($selects_key, 0, 2) == 'p_') {
                    unset($props_goods_alls1[substr($selects_key, 2)]);
                    if ($brands_goods_alls) {
                        $props_goods_alls_selected = array_intersect($cats_goods,$brands_goods_alls);
                    } else {
                        $props_goods_alls_selected = $cats_goods;
                    }
                }
            }
        }

        if($selects_key == 'brand_id'){
            if($props_goods_alls) {
                $props_goods_filter = array_intersect($cats_goods, $brands_goods_alls, $props_goods_alls);
                $brand_goods_filter = array_intersect($cats_goods ,$props_goods_alls);
            }else{
                $props_goods_filter = array_intersect($cats_goods, $brands_goods_alls);
                $brand_goods_filter = $cats_goods;
                $cat_all_brand_filter_flag = $data==2?true:false;
            }
        }elseif (substr($selects_key, 0, 2) == 'p_') {
            if($brands_goods_alls){
                $brand_goods_filter = array_intersect($cats_goods ,$props_goods_alls);
                $props_goods_filter = array_intersect($cats_goods, $brands_goods_alls, $props_goods_alls);
            }else{
//                $brand_goods_filter = $cats_goods;
                $props_goods_filter = $brand_goods_filter = array_intersect($cats_goods ,$props_goods_alls);
//                $props_goods_filter = array_intersect($cats_goods, $props_goods_alls);
            }
        }

        //获取光亮品牌
//        $is_exists = $this->redis->exists('cat_all_brand');
        if($cat_all_brand_filter_flag && $this->redis->exists('cat_all_brand')){
            $this->redis->fetch('cat_all_brand',$cat_all_brand);
            $aProps['brand'] = $cat_all_brand;
            unset($cat_all_brand_filter_flag,$is_exists,$brand_goods_filter);
        }else {
            foreach ($brand_obj as $_bok => $_bov) {
                $brands_obj_goods = $this->redis->setMembers('brand_' . $_bov);
                if ($brands_obj_goods) {
//                $brands_obj_goods  = array_slice($brands_obj_goods,0,1000);
//                $brand_goods_filter  = array_slice($brand_goods_filter,0,1000);
//                if(count($brand_goods_filter)>count($brands_obj_goods)){
//                    $brand_goods_filter_arr = array_chunk($brand_goods_filter,1000);
//                    foreach($brand_goods_filter_arr as $value){
////                        if($this->custom_array_intersect($value,$brands_obj_goods)){
//                        if($this->custom_array_intersect($brands_obj_goods,$value)){
//                            $brands_goods_intersect = true;
//                            break;
//                        }
//                    }
//                }else{
//                    $brands_obj_goods_arr = array_chunk($brands_obj_goods,1000);
//                    foreach($brands_obj_goods_arr as $value){
//                        if($this->custom_array_intersect($brand_goods_filter,$value)){
//                            $brands_goods_intersect = true;
//                            break;
//                        }
//                    }
//                }

                    $brands_goods_intersect = $this->custom_array_intersect($brands_obj_goods, $brand_goods_filter);
                    if ($brands_goods_intersect) {
                        $aProps['brand'][] = $_bov;
                    }


                }
                unset($brands_obj_goods, $brands_goods_intersect, $cat_all_brand_filter_flag);
            }
        }

        foreach($props_obj as $_pok=>$_pov){
            if($props_goods_filter){
                if (substr($selects_key, 2) == $_pov['goods_p']) {
                    $props_goods_filter2 = $props_goods_alls_selected;
                } else {
                    $props_goods_filter2 = $props_goods_filter;
                }
            }else{
                $props_goods_filter2 = $props_goods_filter;
            }
            foreach($_pov['props_value_info'] as $_pokey=>$_poval) {
                $props_obj_goods = $this->redis->setMembers('props_'.$_poval['props_value_id']);
                if(!empty($props_obj_goods) && !empty($props_goods_filter2)) {
//                    $props_obj_goods  = array_slice($props_obj_goods,0,1000);
//                    $props_goods_filter2  = array_slice($props_goods_filter2,0,1000);
                    $array_intersect = $this->custom_array_intersect($props_obj_goods,$props_goods_filter2 );
//                    $array_intersect = array_intersect($props_obj_goods,$props_goods_filter2 );
                    if($array_intersect ){
                        $aProps['props'][$_pov['goods_p']]['goods_p'] = $_pov['goods_p'];
                        $aProps['props'][$_pov['goods_p']]['options'][$_poval['props_value_id']] = $_poval['props_value_id'];

                    }
                }
                unset($props_obj_goods);
            }
            unset($props_goods_filter2);
        }
        if(isset($goods_num) && $goods_num == 0){
            $aProps['brand'] = '';
        }
        echo json_encode($aProps , true);exit();
    }
    /*
     *获取商品的某个分类的属性
     */
    private function _get_cat($params){
        if(!empty($params['props_value_id'])) {
            $this->_filter_props($params);
        }

        $this->redis->fetch('cats',$cats_obj);
        $cats_obj = json_decode($cats_obj, true);

        $this->redis->fetch('props',$props_obj);
        $props_obj = json_decode($props_obj, true);

        $this->redis->fetch('brands',$brands_obj);
        $brands_obj = explode(',', $brands_obj);

        $cat_id = 'cat_'.$params['cat_id'];

        //找属性
        foreach($props_obj as $_pk=>$_pv){
            foreach($_pv['props_value_info'] as $_pkey=>$_pval){
                $cur_props_goods = 'props_'.$_pval['props_value_id'];
                $inter_set = $this->redis->setInter( array($cat_id, $cur_props_goods) );
                if(  $inter_set[0]  ){
                    $aProps['props'][$_pv['goods_p']]['goods_p'] = $_pv['goods_p'];
                    $aProps['props'][$_pv['goods_p']]['options'][$_pval['props_value_id']] = $_pval['props_value_id'];
                }
                unset($cur_props_goods,$inter_set);
            }
        }
        //找品牌
        foreach($brands_obj as $_bk=>$_bv){
            $cur_brand_goods = 'brand_'.$_bv ;
            $inter_set = $this->redis->setInter( array($cat_id, $cur_brand_goods) );
            if(  $inter_set[0]  ){
                $aProps['brand'][] = $_bv;
            }
            unset($cur_brand_goods,$inter_set);
        }
        echo json_encode($aProps , true);exit();
    }
    /*
     * 获取商品的虚拟分类属性
     * */
    private function _get_virtual($params){
        if(!empty($params['props_value_id'])) {
            $this->_filter_props($params,1);
        }

        //虚拟的商品
        $virtual_goods = 'virtual_goods_'.$params['virtual_id'];
        if(!$this->redis->exists($virtual_goods)){
            $virtual_goods = 'cat_3';
        }
        //属性
        $this->redis->fetch('props',$props);
        $props = json_decode($props,true);
        //品牌
        $this->redis->fetch('brands',$brands);
        $brands = explode(',', $brands);

        foreach($props as $_pk=>$_pv){
            foreach($_pv['props_value_info'] as $_pkey=>$_pval){
                $cur_props_goods = 'props_'.$_pval['props_value_id'];
                $inter_set = $this->redis->setInter(array($virtual_goods, $cur_props_goods));
                if( $inter_set[0] ){
                    $aProps['props'][$_pv['goods_p']]['goods_p'] = $_pv['goods_p'];
                    $aProps['props'][$_pv['goods_p']]['options'][$_pval['props_value_id']] = $_pval['props_value_id'];
                }
                unset($cur_props_goods,$inter_set);
            }
        }
        foreach($brands as $_bok=>$_bov){
            $cur_brands_goods = 'brand_' . $_bov;
            $inter_set = $this->redis->setInter(array($virtual_goods, $cur_brands_goods));
            if ( $inter_set[0] ) {
                $aProps['brand'][] = $_bov;
            }
            unset($cur_brands_goods,$inter_set);
        }

        echo json_encode($aProps, true);exit();
    }

    private function _get_cats_goods($params='',$data=''){
        //-------分类下的商品id-----------
        $this->redis->fetch('cats',$cats);
        $cats = json_decode($cats, true);
        $cats_goods = '';
        if(!$params || $params=='NaN') {
            if($data) {
                $data = explode(',',$data);
                foreach ($cats as $_ck => $_cv) {
                    if(in_array($_cv['cat_id'],$data)) {
                        foreach ($_cv['child_catInfo'] as $_ckey => $_cval) {
                            if (in_array($_cval['cat_id'], $data)) {
                                $this->redis->fetch('cat_' . $_cval['cat_id'], $cats_child_goods);
                                if($cats_child_goods) {
                                    $cats_goods .= $cats_child_goods . ',';
                                }
                            }else{
                                $this->redis->fetch('cat_' . $_cval['cat_id'], $cats_child_goods);
                                if($cats_child_goods) {
                                    $cats_goods .= $cats_child_goods . ',';
                                }
                            }
                        }
                    }else{
                        foreach ($_cv['child_catInfo'] as $_ckey => $_cval) {
                            if (in_array($_cval['cat_id'], $data)) {
                                $this->redis->fetch('cat_' . $_cval['cat_id'], $cats_child_goods);
                                if($cats_child_goods) {
                                    $cats_goods .= $cats_child_goods . ',';
                                }
                            }
                        }
                    }
                }
            }else{
                foreach ($cats as $_ck => $_cv) {
                    foreach ($_cv['child_catInfo'] as $_ckey => $_cval) {
                        $this->redis->fetch('cat_' . $_cval['cat_id'], $cats_child_goods);
                        if($cats_child_goods) {
                            $cats_goods .= $cats_child_goods . ',';
                        }
                    }
                }
            }
        }else{
            foreach ($cats as $_ck => $_cv) {
                if($_cv['cat_id'] == $params){
                    foreach ($_cv['child_catInfo'] as $_ckey => $_cval) {
                        $this->redis->fetch('cat_' . $_cval['cat_id'], $cats_child_goods);
                        if($cats_child_goods) {
                            $cats_goods .= $cats_child_goods . ',';
                        }
                    }
                    break;
                }
            }
            if( !$cats_goods ) {
                $this->redis->fetch('cat_' . $params, $cats_child_goods);
                if($cats_child_goods) {
                    $cats_goods .= $cats_child_goods . ',';
                }
            }
        }
        $cats_goods = array_unique(explode(',',trim($cats_goods,',')));
        return $cats_goods;
        //-------分类下的商品id end-----------
    }

    /*
     * 获取商品的全部属性
     * */
    private function _get_all($params){
        if(!empty($params['props_value_id'])) {
            $this->_filter_props($params,2);
        }
        //-------分类下的商品id-----------
        $cats_goods = 'cat_3';
        //-------分类下的商品id end-----------

        //-------属性下的商品id-----------
        $this->redis->fetch('props',$props);
        $props = json_decode($props, true);

        foreach($props as $_sk=>$_sv){
            $goods_p = $_sv['goods_p'];
            foreach($_sv['props_value_info'] as $_skey=>$_sval){
                $props_child_goods = 'props_'.$_sval['props_value_id'];
                $inter_set = $this->redis->setInter( array($cats_goods, $props_child_goods) );
                if ( $inter_set[0] ) {
                    $data['props'][$goods_p]['goods_p'] = $goods_p;
                    $data['props'][$goods_p]['options'][$_sval['props_value_id']] = $_sval['props_value_id'] ;
                }
            }
            unset($goods_p,$props_child_goods,$inter_set);
        }
        //-------属性下的商品id end-----------

        //-------品牌下的商品id-----------
        $this->redis->fetch('brands',$brands);
        $brands = explode(',', $brands);

        foreach($brands as $_bk=>$_bv){
            $brands_child_goods = 'brand_'.$_bv;
            $inter_set = $this->redis->setInter( array($cats_goods, $brands_child_goods) ) ;
            if(  $inter_set[0] ){
                $data['brand'][] = $_bv ;
            }
            unset($brands_child_goods,$inter_set);
        }
        if(!$this->redis->exists('cat_all_brand')){
            $this->redis->store('cat_all_brand',$data['brand']);
            $this->redis->expire('cat_all_brand',3600);
        }

        //-------品牌下的商品id end-----------

        echo json_encode($data, true);exit();
    }

//    public function _pro_gid($parr,$cat_gids){
//        $gids = null;
//        $flag = false;
//
//        foreach($parr as $k=>$v){
//            if(strpos($k,'p_')===0){ //过滤所选属性
//                $flag = true;
//                foreach($v as $key=>$val){
//                    $gid = null;
//                    $this->redis->fetch('props_'.$val,$gid);
//                    $gids[strstr($k,'p_')] .= $gid.',';
//                }
//            }
//            if(strpos($k,'brand')===0){ //过滤所选品牌
//                $flag = true;
//                $bgids='';
//                foreach($v as $key=>$val){
//                    $bgid = null;
//                    $this->redis->fetch('brand_'.$val,$bgid);
//                    $bgids .= $bgid.',';
//                }
//            }
//        }
//        //品牌和cat_goodsid 的交集
//        if(isset($bgids) && !empty($bgid)){
//            $barr=array_intersect($cat_gids,array_unique(explode(',',trim($bgids,','))));
//        }
//
//        //属性和cat_goodsid 的交集
//        if(is_array($gids) && !empty($gids)){
//            foreach ($gids as $key => $value) {
//                $arr[]=array_intersect($cat_gids,array_unique(explode(',',trim($value,','))));
//            }
//        }
//
//        if(count($arr)>1){ //选多个属性情况下，例如颜色加材质
//            $intersect = call_user_func_array('array_intersect', $arr);
//            if(isset($barr) && !empty($barr)){//假如也选择了品牌
//                $intersect = array_intersect($intersect,$barr);
//            }
//        }elseif(count($arr)==1){//只选一个属性情况下
//            $intersect = $arr[0];
//            if(isset($barr) && !empty($barr)){//假如也选择了品牌
//                $intersect = array_intersect($intersect,$barr);
//            }
//        }elseif(is_null($arr) && isset($barr) && !empty($barr)){//只选择了品牌
//            $intersect = $barr;
//        }
//
//        if($flag){
//            return array_unique($intersect);
//        }else{
//            return 'false';
//        }
//    }
    public function custom_array_intersect($param_a, $param_b){
//        if(count($param_a)<count($param_b)){
//            list($param_b,$param_a)=array($param_a,$param_b);
//        }
        $f = array_flip($param_a);
        foreach ($param_b as $k=>$v) {
            if (isset($f[$v])){
                return true;
            }
        }
        return false;
    }
    ////end

}
